package me.zhengjie.nmi;

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.mi.neww.MyEasyGmUtils;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.HttpClientUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 夏洪波
 * @Date: 2024/11/12 16:30
 * @Description:
 */
public class Test8106 {

/*
    private static String pno = "P22011103682";
    private static String LI = "220100";
    private static String CL = "848607E9D9E1CC8E";
    private static String SC = "5936MY";
    private static String USERNAME = "5936";
    private static String PASS = "5936MM";
    private static String SM2PRI = "C2Qf3Dy5AGqV73V92KWcHgeFcq0y+vIAzPQVGDUPwn0=";
    private static String SM2PUB = "BG8Aj4rpx+4lB+2E3/QjftRE0yxKNlHkrJHit6p61oSNDXAVCfL2Qfbdp03czAzXg/p54Rteu3d9jLvdPhzAUfU=";
    private static String serverUrl = "http://ddjk.jlhs.gov.cn:20215/mbs/fmi/fsi/api/callFsiService/callService";
    private static String tokenUrl = "http://ddjk.jlhs.gov.cn:20215/auth/oauth/token";
    */





    private static String pno = "LO220000000010";
    private static String pname = "测试护理机构接口专用一";
    private static String LI = "229900";
    private static String CL = "BD2672C9D52B888B";
    private static String SC = "111111";
    private static String USERNAME = "hlcs220000000010";
    private static String PASS = "111111";
    private static String SM2PRI = "AMspSfr0O7iBjDwkKFRYfG6n0ODSRvEHx96I6h+ypN4l";
    private static String serverUrl = "http://10.47.0.126:8080/mbs/fmi/fsi/api/callHsiService/callService";
    private static String tokenUrl = "http://10.47.0.126:8080/auth/oauth/token";

    /**
     * @param args
     */

    public static void main(String[] args) {

        String token = getToken();
        JSONObject tokenJ = JSONObject.parseObject(token);
        String authorization = tokenJ.getString("access_token");
        System.out.println("[token]:" + authorization);


        String input = "{\"data\": {" +
                "\"longterm_org_servitem_evt_id\":\"LO220000000010-002\"," +
                "\"longterm_org_servitem_id\": \"LO220000000010-AA2\"," +
                "\"nurscare_org_name\": \"测试护理机构接口专用一\"," +
                "\"nurscare_org_id\": \"2021\"," +
                "\"nurscare_org_no\": \"LO220000000010\"," +
                "\"longterm_servitem_id\": \"L22000007900214\"," +
                "\"longterm_servitem_no\": \"L22000007900214-002-002\"," +
                "\"longterm_servitem_name\": \"清洁与舒适护理002\"," +
                "\"longterm_serv_type\": \"07\"," +
                "\"selfpay_prop\": \"0\"," +
                "\"pric_uplmt\": \"50\"," +
                "\"adv_dura_uplmt\": \"20\"," +
                "\"nurscare_org_servitem_no\": \"L22000007900214-002\"," +
                "\"nurscare_org_servitem_name\": \"清洁与舒适护理002\"," +
                "\"nurscare_org_servitem_type\": \"07\"," +
                "\"nurscare_org_admdvs\": \"229900\"," +
                "\"nurscare_org_servitem_pric\": \"23.9\"," +
                "\"nurscare_org_servitem_prcunt_no\": \"01\"," +
                "\"nurscare_org_servitem_memo\": \"\"," +
                "\"enab_stas\": \"1\"," +
                "\"chk_stas\": \"\"," +
                "\"oprt_opnn\": \"\"," +
                "\"rchk_opnn\": \"\"," +
                "\"serv_matt_inst_id\": \"\"," +
                "\"serv_matt_node_inst_id\": \"\"," +
                "\"evt_inst_id\": \"\"," +
                "\"evt_type\": \"\"," +
                "\"rid\": \"\"," +
                "\"crte_time\": \"\"," +
                "\"updt_time\": \"\"," +
                "\"crter_id\": \"\"," +
                "\"crter_name\": \"\"," +
                "\"crte_optins_no\": \"\"," +
                "\"opter_id\": \"\"," +
                "\"opter_name\": \"\"," +
                "\"opt_time\": \"\"," +
                "\"optins_no\": \"\"," +
                "\"admdvs\": \"\"," +
                "\"vali_flag\": \"1\"," +
                "\"data_ver\": \"\"," +
                "\"opt_dot_id\": \"\"," +
                "\"LongtermOrgServitemAttDTO\": \"\"," +
                "\"asst_prdr\": \"\"," +
                "\"asst_avl_cnt\": \"\"," +
                "\"asst_spec\": \"\"," +
                "\"asst_type\": \"\"," +
                "\"asst_prod_type\": \"\"," +
                "\"centre_asst_type\": \"\"," +
                "\"centre_asst_prod_type\": \"\"}}";

        System.out.println(input);
        //签名
        String sign = sign(input);
        //String sign = "";
        //加密
        String sm4Input = encrypt(input);
        //String sm4Input = input;

        System.out.println("sm4Input:"+sm4Input);

        String message = "{" +
                "\"infno\": \"8106\"," +
                "\"msgid\": \"" + pno + ChsUtil.getMsgId() + "\"," +
                "\"mdtrtarea_admvs\": \""+LI+"\"," +
                "\"insuplc_admdvs\": \""+LI+"\"," +
                "\"recer_sys_code\": \"" + ChsInterfaceConfig.recerSysCode001 + "\"," +
                "\"dev_no\": \"\"," +
                "\"dev_safe_info\": \"\"," +
                "\"cainfo\": \"" + sign + "\"," +
                "\"signtype\": \"SM2\"," +
                "\"infver\": \"" + ChsInterfaceConfig.infver + "\"," +
                "\"opter_type\": \"1\"," +
                "\"opter\": \"0001\"," +
                "\"opter_name\": \"test\"," +
                "\"inf_time\": \"" + DateTimeUtil.getDateTime() + "\"," +
                "\"fixmedins_code\": \"" + pno + "\"," +
                "\"fixmedins_name\": \""+pname+"\"," +
                "\"sign_no\": \"1234\"," +
                "\"input\": \"" + sm4Input + "\"}";

        System.out.println("message:"+message);
        JSONObject result = JSONObject.parseObject(handle(message, authorization));
        //System.out.println(message);

        System.out.println(decryption(result.getString("output")));

    }


    /**
     * sm2生成签名
     */
    public static String sign(String inputStr) {
        byte[] privateKey = Base64.getDecoder().decode(SM2PRI);
        byte[] assign = MyEasyGmUtils.signSm3WithSm2(inputStr.getBytes(StandardCharsets.UTF_8),  privateKey);
        return new String(Base64.getEncoder().encode(assign));
    }

    /**
     * sm4加密
     */
    public static String encrypt(String inputStr) {
        byte[] plain = inputStr.getBytes();
        byte[] bs = MyEasyGmUtils.sm4Encrypt(CL.getBytes(StandardCharsets.UTF_8), plain);
        return new String(Base64.getEncoder().encode(bs));
    }

    public static String decryption(String inputStr) {
        byte[] bs = MyEasyGmUtils.sm4Decrypt(CL.getBytes(StandardCharsets.UTF_8), Base64.getDecoder().decode(inputStr));
        return new String(bs);
    }

    /**
     * 获取token
     **/
    public static String getToken() {
        Map<String, String> parmap = new HashMap<String, String>();
        parmap.put("username", USERNAME);
        parmap.put("password", PASS);
        parmap.put("scope", "server");
        parmap.put("grant_type", "password");
        Map<String, String> headermap = new HashMap<String, String>();
        headermap.put("Authorization", "Basic " + Base64.getEncoder().encodeToString((CL + ":" + SC).getBytes(StandardCharsets.UTF_8)));
        headermap.put("Content-Type", "application/x-www-form-urlencoded");
        String result = HttpClientUtils.sendIOPost(tokenUrl, parmap, headermap, Charset.forName("UTF-8"), null);
        System.out.println("[tokenResult]:" + result);
        if (!"".equals(result)) {
            JSONObject json = JSONObject.parseObject(result);
            //有code为错误情况
            if (json.getString("code") == null) {
                System.out.println(json.toJSONString());
            }
        } else {
            result = "{err_msg: '获取token失败,机构信息有误或未进入省平台', code: 500}";
        }
        return result;
    }

    public static String handle(String message, String authorization) {
        Map<String, String> headermap = new HashMap<String, String>();

        headermap.put("Authorization", "Bearer " + authorization);
        headermap.put("Content-Type", "application/json;charset=utf8");
        String result = HttpClientUtils.sendIOPost(serverUrl, null, headermap, Charset.forName("UTF-8"), message);
        System.out.println("[result]" + result);
        return result;
    }


}
